<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Manager extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('managermodel');
		
	}
	
	public function realtime($graphoption='summary-pay'){
		
		$realtimedata = $this->managermodel->getRealtimeUploadDefault();
		$this->load->model('optionmodel');
		$data['pos_option'] = $this->optionmodel->getPosOption();
		$data['realtimeUpload'] = $realtimedata;
		$currenttimezone = date_default_timezone_get();
		date_default_timezone_set("UTC");
		
		foreach ( $data['pos_option'] as $option){
			$dat[$option['posname']] = array();
			$poscolor[$option['posname']] = $option['poscolor'];
		}
		if($graphoption == 'summary-pay'){
		foreach ($realtimedata as $row){
			
			array_push($dat[$row['posname']], array('dateupload' => gmdate(strtotime($row['dateupload']))*1000,
												'total' => $row['sum_netbuy']
												));
		}
		}
		else if ($graphoption == 'summary-wg'){
		foreach ($realtimedata as $row){
			
			array_push($dat[$row['posname']], array('dateupload' => gmdate(strtotime($row['dateupload']))*1000,
												'total' => $row['sum_netwg']
												));
		}
		}
		
		date_default_timezone_set($currenttimezone);
		//ksort($dat);
		$data['dat'] = $dat;
		$data['poscolor'] = $poscolor;
		$data['graphoption'] =$graphoption;
		
		$data['role_id']=$this -> session -> userdata('role_id');
		$this->load->view('manager/realtime',$data);
	}
	
public function summary($graphoption='summary-pay'){
		
		$yearsummary = $this->input->get_post('yearsummary');
		if (!$yearsummary) $yearsummary = date('Y');
		$summarydata = $this->managermodel->getSummary($yearsummary);
		$this->load->model('optionmodel');
		$data['pos_option'] = $this->optionmodel->getPosOption();
		$data['pos_count'] = count($data['pos_option']);
		$data['summarydata'] = $summarydata;
		$data['yearsummary'] = $yearsummary;
		foreach ( $data['pos_option'] as $option){
			//!!! dont forget that month in mysql doesnt begin by 0 (jan=>1)
			//create 2 dimension array  [larn][amonth] = total
			$dat[$option['posname']] = array(0,0,0,0,0,0,0,0,0,0,0,0);
			$poscolor[$option['posname']] = $option['poscolor'];
		}
		if($graphoption == 'summary-pay'){
		foreach ($summarydata as $row){
			$dat[$row['posname']][$row['amonth']-1] = $row['month_netbuy'];
//			array_push($dat[$row['posname']], array('amonth' => $row['amonth'],
//												'total' => $row['month_netbuy']
//												));
		}
		}
		else if ($graphoption == 'summary-wg'){
		foreach ($summarydata as $row){
			$dat[$row['posname']][$row['amonth']-1] = $row['month_netwg'];
//			array_push($dat[$row['posname']], array('amonth' => $row['amonth'],
//												'total' => $row['month_netwg']
//												));
		}
		}
		
		//ksort($dat);
		$data['dat'] = $dat;
		$data['poscolor'] = $poscolor;
		$data['graphoption'] =$graphoption;
		$data['role_id']=$this -> session -> userdata('role_id');
		$this->load->view('manager/summary',$data);
	}
	
public function range($graphoption='summary-pay'){
		
		$pos = isset_or($this->input->post('pos'),'');
		$date = isset_or($this->input->post('date'),'');
		$realtimedata = $this->managermodel->getRangeUpload($pos,$date);
		$this->load->model('optionmodel');
		$data['pos_option'] = $this->optionmodel->getPosOption();
		$data['currentpos'] = $pos;
		$data['currentdate'] = $date;
		$data['realtimeUpload'] = $realtimedata;
		$currenttimezone = date_default_timezone_get();
		date_default_timezone_set("UTC");
		
		foreach ( $data['pos_option'] as $option){
			$dat[$option['posname']] = array();
			$poscolor[$option['posname']] = $option['poscolor'];
		}
		if($graphoption == 'summary-pay'){
		foreach ($realtimedata as $row){
			
			array_push($dat[$row['posname']], array('dateupload' => gmdate(strtotime($row['dateupload']))*1000,
												'total' => $row['sum_netbuy']
												));
		}
		}
		else if ($graphoption == 'summary-wg'){
		foreach ($realtimedata as $row){
			
			array_push($dat[$row['posname']], array('dateupload' => gmdate(strtotime($row['dateupload']))*1000,
												'total' => $row['sum_netwg']
												));
		}
		}
		
		date_default_timezone_set($currenttimezone);
		//ksort($dat);
		$data['dat'] = $dat;
		$data['poscolor'] = $poscolor;
		$data['graphoption'] =$graphoption;
		$data['role_id']=$this -> session -> userdata('role_id');
		$this->load->view('manager/range',$data);
	}
	
	public function dashboard(){
		$data['leftDashboard'] = $this->_getRealtimeDashboard();
		$data['nowmenu']="manager";
		$data['title']="Supply Chain Management -- Dashboard";
		
		$data['role_id']=$this -> session -> userdata('role_id');
		if($this -> session -> userdata('role_id')==1){
			$this->load->view('manager/dashboard',$data);
		}else if($this -> session -> userdata('role_id')==5){
			redirect('land', 'location', 301);
		}else if($this -> session -> userdata('role_id')==6){
			redirect('manager/realtime', 'location', 301);
		}
	}
	
	function _getRealtimeDashboard($graphoption='summary-pay'){
		$realtimedata = $this->managermodel->getRealtimeUploadDefault(5);
		$this->load->model('optionmodel');
		$data['pos_option'] = $this->optionmodel->getPosOption();
		$data['realtimeUpload'] = $realtimedata;
		
		//graph
		$currenttimezone = date_default_timezone_get();
		date_default_timezone_set("UTC");
		
		foreach ( $data['pos_option'] as $option){
			$dat[$option['posname']] = array();
			$poscolor[$option['posname']] = $option['poscolor'];
		}
		if($graphoption == 'summary-pay'){
		foreach ($realtimedata as $row){
			
			array_push($dat[$row['posname']], array('dateupload' => gmdate(strtotime($row['dateupload']))*1000,
												'total' => $row['sum_netbuy']
												));
		}
		}
		else if ($graphoption == 'summary-wg'){
		foreach ($realtimedata as $row){
			
			array_push($dat[$row['posname']], array('dateupload' => gmdate(strtotime($row['dateupload']))*1000,
												'total' => $row['sum_netwg']
												));
		}
		}
		
		date_default_timezone_set($currenttimezone);
		//ksort($dat);
		$data['dat'] = $dat;
		$data['poscolor'] = $poscolor;
		$data['graphoption'] =$graphoption;
		
		return $data;
	}
	
	
	public function supplypayment(){
		$this->load->model('optionmodel');
		$data['pos_option'] = $this->optionmodel->getPosOption();
		$data['role_id']=$this -> session -> userdata('role_id');
		$this->load->view('manager/supplypayment',$data);
	}
	
	public function savesupplypayment(){
		$pos = $this->input->post('pos');
		$money = $this->input->post('money');
		$paymenttype = $this->input->post('paymenttype');
		$bankname = $this->input->post('bankname');
		
		if($this->managermodel->save_supply_payment($pos,$money,$paymenttype,$bankname)){
			redirect('manager/supplybalance?pos='.$pos);
		}else{
			echo 'error';
		}
	}
	
	public function supplybalance(){
		$currentpos = $this->input->get_post('pos');
		if ( ! $currentpos) $currentpos = 1;
		$data['currentpos'] = $currentpos;
		$this->load->model('optionmodel');
		$data['pos_option'] = $this->optionmodel->getPosOption();
		
		$data['supplybalance'] = $this->managermodel->getSupplyBalance($currentpos);
		$data['role_id']=$this -> session -> userdata('role_id');
		$this->load->view('manager/supplybalance',$data);
	}
	
	public function paymentdetail($paid_id){
		$paymentdetail = $this->managermodel->getPaymentDetail($paid_id);
		print_r($paymentdetail);
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */